<template>
  <van-icon
    color="#777"
    :name="value === 1 ? 'good-job' : 'good-job-o'"
    :class="{ like: value === 1 }"
    :loading="loading"
    @click="onLike"
  />
</template>

<script>
import { addLike, deleteLike } from "@/api/collect.js";
export default {
  props: {
    value: {
      type: Number,
      required: true,
    },
    artId: {
      type: [Number, String, Object],
      required: true,
    },
  },
  data() {
    return {
      loading: false,
    };
  },
  methods: {
    async onLike() {
      this.loading = true;
      let status = -1;
      try {
        if (this.value === 1) {
          // 已点赞,取消点赞
          await deleteLike(this.artId);
        } else {
          // 未点赞,添加点赞
          await addLike(this.artId);
          status = 1;
        }
        //更新视图
        this.$emit("input", status);
        this.$toast(status === 1?'点赞成功':'取消点赞')
      } catch (err) {
        this.$toast.fail("操作失败,请重试");
      }
      this.loading = false;
    },
  },
};
</script>

<style scoped lang="less">
.like {
  color: #e5645f;
}
</style>